<!doctype html>
<html lang="zh-han">
  <head>
    <title>Python攻防 // 小田博客</title>
    <link rel="shortcut icon" href="/favicon.ico" />
    <meta charset="utf-8" />
    <meta name="generator" content="Hugo 0.68.3" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="author" content="John Doe" />
    <meta name="description" content="" />
    <link rel="stylesheet" href="https://tian_828.gitee.io/css/main.min.4a7ec8660f9a44b08c4da97c5f2e31b1192df1d4d0322e65c0dbbc6ecb1b863f.css" />

    
    <meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Python攻防"/>
<meta name="twitter:description" content="python基础 安装python3 1. 用apt安装python3 shell sudo apt-get install python3 update-alternatives --install /usr/bin/python python /usr/bin/python2 100 update-alternatives --install /usr/bin/python python /usr/bin/python3 150 2. 安装python包管理工具pip sudo apt-get install python3-pip 3. 安装好python和pip后，通过pip下载第三方python包 pip install requests Git Git 的工作就是创建和保存你项目的快照及与之后的快照进行对比
Git 常用的是以下 6 个命令：git clone、git push、git add 、git commit、git checkout、git pull !https://www.runoob.com/wp-content/uploads/2015/02/git-command.jpg 注释： workspace：工作区 staging area：暂存区/缓存区 local repository：版本库或本地仓库 remote repository：远程仓库 Git 基本操作 git init #初始化仓库。 git add . #添加文件到暂存区。 git commit #将暂存区内容添加到仓库中。 创建仓库 git init	#初始化仓库 git clone	#拷贝一份远程仓库，也就是下载一个项目 提交与修改 git add	#添加文件到仓库 git status	#查看仓库当前的状态，显示有变更的文件。 git diff	#比较文件的不同，即暂存区和工作区的差异。 git commit	#提交暂存区到本地仓库。 git reset	#回退版本。 git rm	#删除工作区文件。 git mv	#移动或重命名工作区文件 提交日志 git log	#查看历史提交记录 git blame &lt;file&gt;	#以列表形式查看指定文件的历史修改记录 远程操作 git remote	#远程仓库操作 git fetch	#从远程获取代码库 git pull	#下载远程代码并合并 git push	#上传远程代码并合并 Git创建仓库 搭建本地仓库 新建一个文件夹，作为本地仓库，即工作目录，右击选择Git Bash Here，执行初始化命令"/>

    <meta property="og:title" content="Python攻防" />
<meta property="og:description" content="python基础 安装python3 1. 用apt安装python3 shell sudo apt-get install python3 update-alternatives --install /usr/bin/python python /usr/bin/python2 100 update-alternatives --install /usr/bin/python python /usr/bin/python3 150 2. 安装python包管理工具pip sudo apt-get install python3-pip 3. 安装好python和pip后，通过pip下载第三方python包 pip install requests Git Git 的工作就是创建和保存你项目的快照及与之后的快照进行对比
Git 常用的是以下 6 个命令：git clone、git push、git add 、git commit、git checkout、git pull !https://www.runoob.com/wp-content/uploads/2015/02/git-command.jpg 注释： workspace：工作区 staging area：暂存区/缓存区 local repository：版本库或本地仓库 remote repository：远程仓库 Git 基本操作 git init #初始化仓库。 git add . #添加文件到暂存区。 git commit #将暂存区内容添加到仓库中。 创建仓库 git init	#初始化仓库 git clone	#拷贝一份远程仓库，也就是下载一个项目 提交与修改 git add	#添加文件到仓库 git status	#查看仓库当前的状态，显示有变更的文件。 git diff	#比较文件的不同，即暂存区和工作区的差异。 git commit	#提交暂存区到本地仓库。 git reset	#回退版本。 git rm	#删除工作区文件。 git mv	#移动或重命名工作区文件 提交日志 git log	#查看历史提交记录 git blame &lt;file&gt;	#以列表形式查看指定文件的历史修改记录 远程操作 git remote	#远程仓库操作 git fetch	#从远程获取代码库 git pull	#下载远程代码并合并 git push	#上传远程代码并合并 Git创建仓库 搭建本地仓库 新建一个文件夹，作为本地仓库，即工作目录，右击选择Git Bash Here，执行初始化命令" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://tian_828.gitee.io/post/python%E6%94%BB%E9%98%B2/" />
<meta property="article:published_time" content="2021-05-19T17:13:29+08:00" />
<meta property="article:modified_time" content="2021-05-19T17:13:29+08:00" />


  </head>
  <body>
    <header class="app-header">
      <a href="https://tian_828.gitee.io/"><img class="app-header-avatar" src="/avatar.jpg" alt="John Doe" /></a>
      <h1>小田博客</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vehicula turpis sit amet elit pretium.</p>
    </header>
    <main class="app-container">
      
  <article class="post">
    <header class="post-header">
      <h1 class ="post-title">Python攻防</h1>
      <div class="post-meta">
        <div>
          <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-calendar">
  <title>calendar</title>
  <rect x="3" y="4" width="18" height="18" rx="2" ry="2"></rect><line x1="16" y1="2" x2="16" y2="6"></line><line x1="8" y1="2" x2="8" y2="6"></line><line x1="3" y1="10" x2="21" y2="10"></line>
</svg>
          May 19, 2021
        </div>
        <div>
          <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-clock">
  <title>clock</title>
  <circle cx="12" cy="12" r="10"></circle><polyline points="12 6 12 12 16 14"></polyline>
</svg>
          4 min read
        </div>
      </div>
    </header>
    <div class="post-content">
      <h1 id="python基础">python基础</h1>
<h2 id="安装python3">安装python3</h2>
<pre><code>1. 用apt安装python3
shell
sudo apt-get install python3

update-alternatives --install /usr/bin/python python /usr/bin/python2 100
update-alternatives --install /usr/bin/python python /usr/bin/python3 150

2. 安装python包管理工具pip
sudo apt-get install python3-pip

3. 安装好python和pip后，通过pip下载第三方python包
pip install requests
</code></pre><h1 id="git">Git</h1>
<p>Git 的工作就是创建和保存你项目的快照及与之后的快照进行对比</p>
<pre><code>Git 常用的是以下 6 个命令：git clone、git push、git add 、git commit、git checkout、git pull
!https://www.runoob.com/wp-content/uploads/2015/02/git-command.jpg
注释：
workspace：工作区
staging area：暂存区/缓存区
local repository：版本库或本地仓库
remote repository：远程仓库
</code></pre><h2 id="git-基本操作">Git 基本操作</h2>
<pre><code>git init  #初始化仓库。
git add . #添加文件到暂存区。
git commit #将暂存区内容添加到仓库中。
</code></pre><h3 id="创建仓库">创建仓库</h3>
<pre><code>git init	#初始化仓库
git clone	#拷贝一份远程仓库，也就是下载一个项目
</code></pre><h3 id="提交与修改">提交与修改</h3>
<pre><code>git add	#添加文件到仓库
git status	#查看仓库当前的状态，显示有变更的文件。
git diff	#比较文件的不同，即暂存区和工作区的差异。
git commit	#提交暂存区到本地仓库。
git reset	#回退版本。
git rm	#删除工作区文件。
git mv	#移动或重命名工作区文件
</code></pre><h3 id="提交日志">提交日志</h3>
<pre><code>git log	#查看历史提交记录
git blame &lt;file&gt;	#以列表形式查看指定文件的历史修改记录
</code></pre><h3 id="远程操作">远程操作</h3>
<pre><code>git remote	#远程仓库操作
git fetch	#从远程获取代码库
git pull	#下载远程代码并合并
git push	#上传远程代码并合并
</code></pre><h2 id="git创建仓库">Git创建仓库</h2>
<h3 id="搭建本地仓库">搭建本地仓库</h3>
<p>新建一个文件夹，作为本地仓库，即工作目录，右击选择Git Bash Here，执行初始化命令</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">git init
</code></pre></div><h3 id="克隆远程仓库">克隆远程仓库</h3>
<p>将远程服务器上的仓库完全镜像一份至本地</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">git clone <span style="color:#f92672">[</span>url<span style="color:#f92672">]</span>
</code></pre></div><h1 id="scapy">scapy</h1>
<h2 id="安装scapy">安装scapy</h2>
<p>scapy是基于python的包，可以用Python的pip包管理工具下载安装</p>
<p>下载scapy包</p>
<pre><code>pip install scapy
</code></pre><h2 id="scapy应用">scapy应用</h2>
<p>1.创建数据包</p>
<p>scapy的数据包创建按照的是TCP/IP协议的四层参考模型：网络接口层、互联网层、传输层、应用层。Scapy为每一层都写了相应的类，创建数据包时我们只要将这些类实例化，然后调用类的方法或改变类的参数值即可。各个层的协议都有各自的创建函数，如IP()、TCP()、UDP()等</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-py" data-lang="py">r<span style="color:#f92672">=</span>(IP()<span style="color:#f92672">/</span>TCP())
r[IP]<span style="color:#f92672">.</span>dst<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;172.22.92.18&#34;</span>
r[TCP]<span style="color:#f92672">.</span>flags<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;A&#34;</span>
</code></pre></div><p>2.发送并接收数据包</p>
<p>用sr1()函数将构造好的的数据包发送出去，并将目标主机返回的数据包传递给变量rep</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-py" data-lang="py">rep<span style="color:#f92672">=</span>sr1(r)
</code></pre></div><p>3.查看数据包信息</p>
<p>用display查看数据包的信息</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">rep<span style="color:#f92672">.</span>display()
</code></pre></div><h1 id="hugo">Hugo</h1>
<h2 id="hugo下载">hugo下载</h2>
<p>linux 环境</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">    sudo apt-get install hugo
</code></pre></div><h2 id="hugo使用">hugo使用</h2>
<h3 id="1生成站点">1.生成站点</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">    hugo new site hgwork
</code></pre></div><h3 id="2创建一个文章">2.创建一个文章</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">    hugo new post/first.md
</code></pre></div><p>在post目录下创建一个名为firs.md的markdown文件</p>
<h3 id="3下载一个皮肤">3.下载一个皮肤</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">    cd themes
    git clone https://github.com/spf13/hyde.git
</code></pre></div><p>先cd到主题目录下，克隆一个主题到该目录</p>
<h2 id="hugo-blog">hugo blog</h2>
<pre><code># 1. clone gitee pages repository
git clone https://gitee.com/akctf/akctf.git
cd akctf

# 2. hugo site
hugo new site hugo

# 2.1 add theme
cd hugo
# git submodule add https://gitee.com/acezqy/hugo-kiera.git themes/kiera

# 或者在 config.toml 文件中 增加 theme = &quot;ananke&quot;
echo theme = \&quot;ananke\&quot; &gt;&gt; config.toml
# echo theme = \&quot;kiera\&quot; &gt;&gt; config.toml

# 2.2 创建 post
hugo new posts/my-first-post.md

# 2.3 本地测试
hugo server -D

# 2.4 生成静态网站 =&gt; public 文件夹
hugo -D

cd ..
# 3. Push to gitee 把仓库更新到远端
git add .
git commit -m &quot;add hugo dir site&quot;
git push origin master
</code></pre><h3 id="常见错误">常见错误:</h3>
<pre><code>/etc/apt下文件修改需要sudo权限

github 下的 theme 仓库，由于网络无法下载。通过 Gitee 导入再 clone

概念混淆：theme 主页地址，并非其git 仓库地址。

git pages 仓库命名，和最终URL的关系

git pages 部署目录, 应和 hugo/hexo 生成的静态资源保持一直。eg: hugo/public

首次运行 git, 本地 git Push 到 remote, 必须先执行 git config 配置 username &amp; email

hugo/config.toml文件中：

baseUrl 填写： 填写网站地址， 而非 仓库, 且不要忘记斜杠 /(否则出现静态资源404错误)

theme 填写: themes目录下对应 clone 主题

仓库重新clone后， 如何 clone submodule

webIDE 修改后, 本地应 先 pull 再 push

root用户开发危险, (权限过大)
git submodule update --init
git submodule init
git submodule update

需重新hugo -D rebuild publich 目录, 并重新 push代码， 静态资源才会更新并推送到remote
config.toml文件更新后
修改posts 下的 md 文件后
</code></pre><h1 id="tcp">TCP</h1>
<h2 id="tcp_find">tcp_find</h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-py" data-lang="py"><span style="color:#f92672">import</span> time
<span style="color:#f92672">from</span> optparse <span style="color:#f92672">import</span> OptionParser
<span style="color:#f92672">from</span> random <span style="color:#f92672">import</span> randint
<span style="color:#f92672">from</span> scapy.all <span style="color:#f92672">import</span> <span style="color:#f92672">*</span>

<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">Scan</span>(ip):
    <span style="color:#66d9ef">try</span>:
        dport <span style="color:#f92672">=</span> random<span style="color:#f92672">.</span>randint(<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">65535</span>)
        <span style="color:#75715e"># dport = 888</span>
        packet <span style="color:#f92672">=</span> IP(dst<span style="color:#f92672">=</span>ip)<span style="color:#f92672">/</span>TCP(flags<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;A&#34;</span>, dport<span style="color:#f92672">=</span>dport)
        resp <span style="color:#f92672">=</span> sr1(packet, timeout<span style="color:#f92672">=</span><span style="color:#ae81ff">0.5</span>, verbose<span style="color:#f92672">=</span><span style="color:#ae81ff">0</span>)
        <span style="color:#66d9ef">if</span> resp:
            <span style="color:#66d9ef">if</span> int(resp[TCP]<span style="color:#f92672">.</span>flags) <span style="color:#f92672">==</span> <span style="color:#ae81ff">4</span>: <span style="color:#75715e"># RST</span>
                <span style="color:#75715e"># time.sleep(0.1)</span>
                <span style="color:#66d9ef">print</span>(ip <span style="color:#f92672">+</span> <span style="color:#e6db74">&#39; Host is up&#39;</span>)
            <span style="color:#66d9ef">else</span>:
                <span style="color:#66d9ef">print</span>(ip <span style="color:#f92672">+</span> <span style="color:#e6db74">&#39; Host is not RST&#39;</span>)
        <span style="color:#66d9ef">else</span>:
            <span style="color:#66d9ef">print</span>(ip <span style="color:#f92672">+</span> <span style="color:#e6db74">&#39; Host is down&#39;</span>)
    <span style="color:#66d9ef">except</span>:
        exit(<span style="color:#ae81ff">0</span>)


<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">main</span>():
    usage <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;Usage: %prog -i &lt;ip address&gt;&#34;</span>
    parse <span style="color:#f92672">=</span> OptionParser(usage<span style="color:#f92672">=</span>usage)
    parse<span style="color:#f92672">.</span>add_option(<span style="color:#e6db74">&#34;-i&#34;</span>, <span style="color:#e6db74">&#39;--ip&#39;</span>, type<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;string&#34;</span>, dest<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;targetIP&#34;</span>, help<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;specify the IP address&#34;</span>)
    options, args <span style="color:#f92672">=</span> parse<span style="color:#f92672">.</span>parse_args()
    <span style="color:#66d9ef">if</span> <span style="color:#e6db74">&#39;-&#39;</span> <span style="color:#f92672">in</span> options<span style="color:#f92672">.</span>targetIP:
        arr0 <span style="color:#f92672">=</span> options<span style="color:#f92672">.</span>targetIP<span style="color:#f92672">.</span>split(<span style="color:#e6db74">&#39;-&#39;</span>)
        <span style="color:#75715e"># -i 192.168.3.1-120</span>
        <span style="color:#75715e"># arr0 = [&#39;192.168.3.1&#39;, &#39;120&#39;]</span>

        arr <span style="color:#f92672">=</span> arr0[<span style="color:#ae81ff">0</span>]<span style="color:#f92672">.</span>split(<span style="color:#e6db74">&#39;.&#39;</span>)
        <span style="color:#75715e"># arr = [&#39;192&#39;, &#39;168&#39;, &#39;3&#39;, &#39;1&#39;]</span>
        ip_3 <span style="color:#f92672">=</span> arr[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">+</span> <span style="color:#e6db74">&#39;.&#39;</span> <span style="color:#f92672">+</span> arr[<span style="color:#ae81ff">1</span>] <span style="color:#f92672">+</span> <span style="color:#e6db74">&#39;.&#39;</span> <span style="color:#f92672">+</span> arr[<span style="color:#ae81ff">2</span>] <span style="color:#f92672">+</span> <span style="color:#e6db74">&#39;.&#39;</span>
        <span style="color:#75715e"># ip_3 = &#39;192.168.3.&#39;</span>
        
        <span style="color:#66d9ef">for</span> i <span style="color:#f92672">in</span> range(int(arr0[<span style="color:#ae81ff">0</span>]<span style="color:#f92672">.</span>split(<span style="color:#e6db74">&#39;.&#39;</span>)[<span style="color:#ae81ff">3</span>]), int(arr0[<span style="color:#ae81ff">1</span>]) <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>):
            Scan(ip_3 <span style="color:#f92672">+</span> str(i))

    <span style="color:#66d9ef">else</span>:
        Scan(options<span style="color:#f92672">.</span>targetIP)

<span style="color:#66d9ef">if</span> __name__ <span style="color:#f92672">==</span> <span style="color:#e6db74">&#39;__main__&#39;</span>:
    main()
</code></pre></div><h1 id="icmp探测">ICMP探测</h1>
<p>1.ping.bat</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bat" data-lang="bat">@<span style="color:#66d9ef">echo</span> off
<span style="color:#66d9ef">set</span> <span style="color:#66d9ef">/p</span> ip=<span style="color:#e6db74">&#34;please input ip &lt;address&gt;, format is : &#34;</span>192.168.1.<span style="color:#e6db74">&#34; &gt;&gt;&gt;&#34;</span>

<span style="color:#66d9ef">for</span> <span style="color:#66d9ef">/L</span> <span style="color:#ae81ff">%%</span>i <span style="color:#66d9ef">in</span> (<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">255</span>) <span style="color:#66d9ef">do</span> (
ping -n 1 -l 16 -w 100 %ip%<span style="color:#ae81ff">%%</span>i | findstr TTL= &gt; nul &amp;&amp; <span style="color:#66d9ef">echo</span> %ip%<span style="color:#ae81ff">%%</span>i is up. || <span style="color:#66d9ef">echo</span> %ip%<span style="color:#ae81ff">%%</span>i is down.
)

<span style="color:#66d9ef">echo</span> test ping ok ! &amp; <span style="color:#66d9ef">pause</span>
</code></pre></div><p>2.ping.sh</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">$ip
read -p <span style="color:#e6db74">&#34;please input ip: &#34;</span> ip

<span style="color:#66d9ef">for</span> <span style="color:#f92672">((</span>i<span style="color:#f92672">=</span>0;i&lt;<span style="color:#f92672">=</span>255;i++<span style="color:#f92672">))</span>; <span style="color:#66d9ef">do</span>
ping -c <span style="color:#ae81ff">1</span> -W 0.1 $ip$i &gt; /dev/null <span style="color:#f92672">&amp;&amp;</span> echo <span style="color:#e6db74">&#34;</span>$ip$i<span style="color:#e6db74"> is up.&#34;</span> <span style="color:#f92672">||</span> echo <span style="color:#e6db74">&#34;</span>$ip$i<span style="color:#e6db74"> is down&#34;</span>
<span style="color:#66d9ef">done</span>

echo <span style="color:#e6db74">&#34;Complete&#34;</span>
</code></pre></div><p>3.scany.py</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-py" data-lang="py"><span style="color:#f92672">import</span> time
<span style="color:#f92672">from</span> optparse <span style="color:#f92672">import</span> OptionParser
<span style="color:#f92672">from</span> random <span style="color:#f92672">import</span> randint
<span style="color:#f92672">from</span> scapy.all <span style="color:#f92672">import</span> <span style="color:#f92672">*</span>

<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">main</span>(ip):
    <span style="color:#66d9ef">try</span>:
        dport <span style="color:#f92672">=</span> random<span style="color:#f92672">.</span>randint(<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">65535</span>)
        <span style="color:#75715e"># dport = 888</span>
        packet <span style="color:#f92672">=</span> IP(dst<span style="color:#f92672">=</span>ip)<span style="color:#f92672">/</span>TCP(flags<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;A&#34;</span>, dport<span style="color:#f92672">=</span>dport)
        resp <span style="color:#f92672">=</span> sr1(packet, timeout<span style="color:#f92672">=</span><span style="color:#ae81ff">0.5</span>, verbose<span style="color:#f92672">=</span><span style="color:#ae81ff">0</span>)
        <span style="color:#66d9ef">if</span> resp:
            <span style="color:#66d9ef">if</span> int(resp[TCP]<span style="color:#f92672">.</span>flags) <span style="color:#f92672">==</span> <span style="color:#ae81ff">4</span>: <span style="color:#75715e"># RST</span>
                <span style="color:#75715e"># time.sleep(0.1)</span>
                <span style="color:#66d9ef">print</span>(ip <span style="color:#f92672">+</span> <span style="color:#e6db74">&#39; Host is up&#39;</span>)
            <span style="color:#66d9ef">else</span>:
                <span style="color:#66d9ef">print</span>(ip <span style="color:#f92672">+</span> <span style="color:#e6db74">&#39; Host is not RST&#39;</span>)
        <span style="color:#66d9ef">else</span>:
            <span style="color:#66d9ef">print</span>(ip <span style="color:#f92672">+</span> <span style="color:#e6db74">&#39; Host is down&#39;</span>)
    <span style="color:#66d9ef">except</span>:
        exit(<span style="color:#ae81ff">0</span>)

<span style="color:#66d9ef">if</span> __name__ <span style="color:#f92672">==</span> <span style="color:#e6db74">&#39;__main__&#39;</span>:
    main(<span style="color:#e6db74">&#39;192.168.3.5&#39;</span>)
</code></pre></div><h1 id="arp">ARP</h1>
<h2 id="python实现">python实现</h2>
<p>1.用python代码实现网络主机发现</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-py" data-lang="py"><span style="color:#f92672">import</span> socket
<span style="color:#f92672">from</span> scapy.all <span style="color:#f92672">import</span> <span style="color:#f92672">*</span>
<span style="color:#f92672">from</span> getmac <span style="color:#f92672">import</span> get_mac_address

<span style="color:#f92672">import</span> socket<span style="color:#f92672">,</span>struct<span style="color:#f92672">,</span>fcntl

<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">get_ip</span>(ifname):
    s <span style="color:#f92672">=</span> socket<span style="color:#f92672">.</span>socket(socket<span style="color:#f92672">.</span>AF_INET, socket<span style="color:#f92672">.</span>SOCK_DGRAM)
    <span style="color:#66d9ef">return</span> socket<span style="color:#f92672">.</span>inet_ntoa(fcntl<span style="color:#f92672">.</span>ioctl(s<span style="color:#f92672">.</span>fileno(), <span style="color:#ae81ff">0x8915</span>, struct<span style="color:#f92672">.</span>pack(<span style="color:#e6db74">&#39;256s&#39;</span>, bytes(ifname[:<span style="color:#ae81ff">15</span>],<span style="color:#e6db74">&#39;utf-8&#39;</span>)))[<span style="color:#ae81ff">20</span>:<span style="color:#ae81ff">24</span>])


<span style="color:#75715e"># pip install getmac</span>

<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">HostAddress</span>():
    eth_mac <span style="color:#f92672">=</span> get_mac_address(interface<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;eth0&#34;</span>)
    hostname <span style="color:#f92672">=</span> socket<span style="color:#f92672">.</span>gethostname()
    ip <span style="color:#f92672">=</span> get_ip(<span style="color:#e6db74">&#34;eth0&#34;</span>)
    <span style="color:#66d9ef">return</span> (eth_mac, ip)

<span style="color:#75715e"># ARP scapy</span>

<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">ArpScan</span>():
    mac <span style="color:#f92672">=</span> HostAddress()[<span style="color:#ae81ff">0</span>]
    ip <span style="color:#f92672">=</span> HostAddress()[<span style="color:#ae81ff">1</span>]
    iface <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;eth0&#34;</span>
    
    
    <span style="color:#66d9ef">print</span>(mac, ip)

    ipSplit <span style="color:#f92672">=</span> ip<span style="color:#f92672">.</span>split(<span style="color:#e6db74">&#39;.&#39;</span>) <span style="color:#75715e"># 192.168.3.30</span>
    ips <span style="color:#f92672">=</span> []
    <span style="color:#66d9ef">for</span> i <span style="color:#f92672">in</span> range(<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">255</span>):
        <span style="color:#75715e"># [192, 168, 3, 30]</span>
        ip <span style="color:#f92672">=</span> ipSplit[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">+</span> <span style="color:#e6db74">&#39;.&#39;</span> <span style="color:#f92672">+</span> ipSplit[<span style="color:#ae81ff">1</span>] <span style="color:#f92672">+</span> <span style="color:#e6db74">&#39;.&#39;</span> <span style="color:#f92672">+</span> ipSplit[<span style="color:#ae81ff">2</span>] <span style="color:#f92672">+</span> <span style="color:#e6db74">&#39;.&#39;</span> <span style="color:#f92672">+</span> str(i)
        ips<span style="color:#f92672">.</span>append(ip)

    resp <span style="color:#f92672">=</span> srp(Ether(src<span style="color:#f92672">=</span>mac, dst<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;FF:FF:FF:FF:FF:FF&#34;</span>)<span style="color:#f92672">/</span>ARP(op<span style="color:#f92672">=</span><span style="color:#ae81ff">1</span>,hwsrc<span style="color:#f92672">=</span>mac,hwdst<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;00:00:00:00:00:00&#39;</span>,pdst<span style="color:#f92672">=</span>ips),iface<span style="color:#f92672">=</span>iface,timeout<span style="color:#f92672">=</span><span style="color:#ae81ff">2</span>,verbose<span style="color:#f92672">=</span>False)
    respAns <span style="color:#f92672">=</span> resp[<span style="color:#ae81ff">0</span>]<span style="color:#f92672">.</span>res

    lives <span style="color:#f92672">=</span> []
    <span style="color:#75715e"># recv packet amount</span>
    nums <span style="color:#f92672">=</span> len(respAns)
    <span style="color:#66d9ef">print</span>(<span style="color:#e6db74">&#34;nums: &#34;</span>,nums)
    <span style="color:#66d9ef">print</span>(<span style="color:#e6db74">&#34;++++++++++++&#34;</span>)
    <span style="color:#66d9ef">print</span>(<span style="color:#e6db74">&#34;ARP Result: &#34;</span>)
    <span style="color:#66d9ef">print</span>(<span style="color:#e6db74">&#34;===============&#34;</span>)

    <span style="color:#66d9ef">for</span> x <span style="color:#f92672">in</span> range(nums):
        IP <span style="color:#f92672">=</span> respAns[x][<span style="color:#ae81ff">1</span>][<span style="color:#ae81ff">1</span>]<span style="color:#f92672">.</span>fields[<span style="color:#e6db74">&#39;psrc&#39;</span>]
        MAC <span style="color:#f92672">=</span> respAns[x][<span style="color:#ae81ff">1</span>][<span style="color:#ae81ff">1</span>]<span style="color:#f92672">.</span>fields[<span style="color:#e6db74">&#39;hwsrc&#39;</span>]
        lives<span style="color:#f92672">.</span>append([IP,MAC])
        <span style="color:#66d9ef">print</span>(<span style="color:#e6db74">&#34;Alive:&#34;</span>, x<span style="color:#f92672">+</span><span style="color:#ae81ff">1</span>)

        <span style="color:#66d9ef">print</span>(<span style="color:#e6db74">&#34;IP: &#34;</span> <span style="color:#f92672">+</span> IP <span style="color:#f92672">+</span> <span style="color:#e6db74">&#39;</span><span style="color:#ae81ff">\n\n</span><span style="color:#e6db74">&#39;</span> <span style="color:#f92672">+</span> <span style="color:#e6db74">&#34;MAC: &#34;</span> <span style="color:#f92672">+</span> MAC)
        <span style="color:#66d9ef">print</span>(<span style="color:#e6db74">&#34;===============&#34;</span>)

    <span style="color:#66d9ef">print</span>(<span style="color:#e6db74">&#34;++++++++++++&#34;</span>)

<span style="color:#66d9ef">if</span> __name__ <span style="color:#f92672">==</span> <span style="color:#e6db74">&#39;__main__&#39;</span>:
    ArpScan()

</code></pre></div><p>2.通过终端运行（文件名为arp_find.py）</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">sudo -E python3 arp_find.py
</code></pre></div>
    </div>
    <div class="post-footer">
      
    </div>
  </article>

    </main>
  </body>
</html>
